/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Section;

import Designation.*;
import java.sql.PreparedStatement;
import connectDB.ConnectDB;
import java.sql.*;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author ADMIN
 */
public class Sec_Update extends javax.swing.JFrame {

    ConnectDB connect = new ConnectDB();
    String id;
    String name;
    String inch;
    String deptno;

    public Sec_Update(String id) {
        connect.getConnect();
        initComponents();
        this.id = id;
        this.name = name;
        this.inch = inch;
        this.deptno = deptno;
        loadData();
        cbbDepNoUpdate.removeAllItems();
        setCbb();

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        txtSectIdUpdate = new javax.swing.JTextField();
        txtSectNameUpdate = new javax.swing.JTextField();
        btnSectUpdate = new javax.swing.JButton();
        btnSectCancel = new javax.swing.JButton();
        txtSectInchUpdate = new javax.swing.JTextField();
        cbbDepNoUpdate = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        txtSectIdUpdate.setEnabled(false);

        txtSectNameUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtSectNameUpdateActionPerformed(evt);
            }
        });

        btnSectUpdate.setText("Update");
        btnSectUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSectUpdateActionPerformed(evt);
            }
        });

        btnSectCancel.setText("Cancel");
        btnSectCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSectCancelActionPerformed(evt);
            }
        });

        txtSectInchUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtSectInchUpdateActionPerformed(evt);
            }
        });

        cbbDepNoUpdate.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cbbDepNoUpdate.setEnabled(false);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(70, 70, 70)
                .addComponent(btnSectUpdate, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 122, Short.MAX_VALUE)
                .addComponent(btnSectCancel)
                .addGap(72, 72, 72))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(cbbDepNoUpdate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(txtSectInchUpdate, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(txtSectIdUpdate, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(txtSectNameUpdate, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(103, 103, 103))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(45, 45, 45)
                .addComponent(txtSectIdUpdate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(txtSectNameUpdate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(txtSectInchUpdate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(cbbDepNoUpdate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(37, 37, 37)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnSectUpdate)
                    .addComponent(btnSectCancel))
                .addContainerGap(61, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void txtSectNameUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtSectNameUpdateActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtSectNameUpdateActionPerformed

    private void btnSectCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSectCancelActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_btnSectCancelActionPerformed

    private void btnSectUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSectUpdateActionPerformed
        // TODO add your handling code here:    
        updateSect();
    }//GEN-LAST:event_btnSectUpdateActionPerformed

    private void txtSectInchUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtSectInchUpdateActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtSectInchUpdateActionPerformed
    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnSectCancel;
    private javax.swing.JButton btnSectUpdate;
    public javax.swing.JComboBox cbbDepNoUpdate;
    private javax.swing.JTextField txtSectIdUpdate;
    private javax.swing.JTextField txtSectInchUpdate;
    private javax.swing.JTextField txtSectNameUpdate;
    // End of variables declaration//GEN-END:variables

    private void updateSect() {

        int i = JOptionPane.showConfirmDialog(this, "Are you sure?", "Yes or No", 0);
        if (i == JOptionPane.YES_OPTION) {
            try {
                String query = "UPDATE Sections SET SECTID = ? "
                        + "WHERE SECTID = ? ";
                PreparedStatement pst = connect.cn.prepareStatement(query);

                //pst.setString(1, id);
                pst.setString(1, name);
                pst.setString(2, inch);

                pst.executeUpdate();
                JOptionPane.showMessageDialog(this, "Update success");
                this.dispose();
            } catch (Exception e) {
                e.printStackTrace();
                JOptionPane.showMessageDialog(this, "Delete fail");
            }

        }
        if(i==JOptionPane.NO_OPTION){
            JOptionPane.showMessageDialog(this, "Please choose the object that you want to update, again!!!");
            this.dispose();
        }
    }

    private void loadData() {
        try {
            String query1 = "Select * from Sections where SECTID = ?";
            PreparedStatement pst = connect.cn.prepareStatement(query1);

            pst.setString(1, id);

            ResultSet rs = pst.executeQuery();
            rs.next();
            txtSectIdUpdate.setText(rs.getString("SECTID"));
            txtSectNameUpdate.setText(rs.getString("SECTNAME"));
            txtSectInchUpdate.setText(rs.getString("SECTINCH"));
            cbbDepNoUpdate.setSelectedItem("DEPTNO");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void setCbb() {
        cbbDepNoUpdate.removeAllItems();
        try {

            String query2 = "Select DEPTNO from Sections";
            Statement st = connect.cn.createStatement();
            ResultSet rs1 = st.executeQuery(query2);
            while (rs1.next()) {
                cbbDepNoUpdate.addItem(rs1.getString("DEPTNO"));
                cbbDepNoUpdate.setSelectedItem(deptno);
            }
        } catch (SQLException ex) {
            Logger.getLogger(Sec_Insert.class.getName()).log(Level.SEVERE, null, ex);

        }

    }
}
